home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 24
/
Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso
/
Aminet
/
comm
/
mail
/
Mutt089src.lha
/
Mutt-0.89i-AMIGA
/
src
/
sendlib.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-01-28
|
35KB
|
1,533 lines
/*
* Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "mutt.h"
#include "rfc2047.h"
#include "send.h"
#include "mx.h"
#include "mime.h"
#include "mailbox.h"
#include "state.h"
#include "parse.h"
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <sys/stat.h>
#include <signal.h>
#define DISPOSITION(X) X==DISPATTACH?"attachment":"inline"
extern char *tspecials;
char B64Chars[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '+', '/'
};
static void transform_to_7bit (BODY *a, FILE *fpin);
static void encode_quoted (FILE * fin, FILE *fout, int istext)
{
int c, linelen = 0;
char line[77], savechar;
while ((c = fgetc (fin)) != EOF)
{
/* Escape lines that begin with "the message separator". */
if (linelen == 5 && !strncmp ("From ", line, 5))
{
strfcpy (line, "=46rom ", sizeof (line));
linelen = 7;
}
else if (linelen == 1 && line[0] == '.')
{
strfcpy (line, "=2E", sizeof (line));
linelen = 3;
}
/* Wrap the line if needed. */
if (linelen == 76 && ((istext && c != '\n') || !istext))
{
/* If the last character is "quoted", then be sure to move all three
* characters to the next line. Otherwise, just move the last
* character...
*/
if (line[linelen-3] == '=')
{
line[linelen-3] = 0;
fputs (line, fout);
fputs ("=\n", fout);
line[linelen] = 0;
line[0] = '=';
line[1] = line[linelen-2];
line[2] = line[linelen-1];
linelen = 3;
}
else
{
savechar = line[linelen-1];
line[linelen-1] = '=';
line[linelen] = 0;
fputs (line, fout);
fputc ('\n', fout);
line[0] = savechar;
linelen = 1;
}
}
if (c == '\n' && istext)
{
/* Check to make sure there is no trailing space on this line. */
if (line[linelen-1] == ' ' || line[linelen-1] == '\t')
{
if (linelen < 74)
{
sprintf (line+linelen-1, "=%2.2X", line[linelen-1]);
fputs (line, fout);
}
else
{
int savechar = line[linelen-1];
line[linelen-1] = '=';
line[linelen] = 0;
fputs (line, fout);
fprintf (fout, "\n=%2.2X", savechar);
}
}
else
{
line[linelen] = 0;
fputs (line, fout);
}
fputc ('\n', fout);
linelen = 0;
}
else if (c != 9 && (c < 32 || c > 126 || c == '='))
{
/* Check to make sure there is enough room for the quoted character.
* If not, wrap to the next line.
*/
if (linelen > 73)
{
line[linelen++] = '=';
line[linelen] = 0;
fputs (line, fout);
fputc ('\n', fout);
linelen = 0;
}
sprintf (line+linelen,"=%2.2X", c);
linelen += 3;
}
else
{
/* Don't worry about wrapping the line here. That will happen during
* the next iteration when I'll also know what the next character is.
*/
line[linelen++] = c;
}
}
/* Take care of anything left in the buffer */
if (linelen > 0)
{
if (line[linelen-1] == ' ' || line[linelen-1] == '\t')
{
/* take care of trailing whitespace */
if (linelen < 74)
sprintf (line+linelen-1, "=%2.2X", line[linelen-1]);
else
{
savechar = line[linelen-1];
line[linelen-1] = '=';
line[linelen] = 0;
fputs (line, fout);
fputc ('\n', fout);
sprintf (line, "=%2.2X", savechar);
}
}
else
line[linelen] = 0;
fputs (line, fout);
}
}
static void encode_base64 (FILE * fin, FILE *fout, int istext)
{
int c1, c2, c3, ch;
int insert_newline = 0;
int linelen = 0;
FOREVER
{
if (istext)
{
if (insert_newline)
{
c1 = '\n';
insert_newline = 0;
c2 = fgetc(fin);
if (c2 == '\n')
{
c2 = '\r';
c3 = '\n';
}
else
{
c3 = fgetc(fin);
if (c3 == '\n')
{
c3 = '\r';
insert_newline = 1;
}
}
}
else
{
c1 = fgetc(fin);
if (c1 == '\n')
{
c1 = '\r';
c2 = '\n';
c3 = fgetc(fin);
if (c3 == '\n')
{
c3 = '\r';
insert_newline = 1;
}
}
else
{
c2 = fgetc(fin);
if (c2 == '\n')
{
c2 = '\r';
c3 = '\n';
}
else
{
c3 = fgetc(fin);
if (c3 == '\n')
{
c3 = '\r';
insert_newline = 1;
}
}
}
}
}
else /* !istext */
{
if ((c1 = fgetc(fin)) == EOF)
break;
c2 = fgetc(fin);
c3 = fgetc(fin);
}
if (linelen + 4 >= 76)
{
fputc('\n', fout);
linelen = 0;
}
ch = c1 >> 2;
fputc (B64Chars[ch], fout);
if (c2 != EOF)
{
ch = ((c1 & 0x3) << 4) | (c2 >> 4);
fputc (B64Chars[ch], fout);
}
else
{
ch = (c1 & 0x3) << 4;
fputc (B64Chars[ch], fout);
fputs("==", fout);
break;
}
if (c3 != EOF)
{
ch = ((c2 & 0xf) << 2) | (c3 >> 6);
fputc (B64Chars[ch], fout);
}
else
{
ch = (c2 & 0xf) << 2;
fputc(B64Chars[ch], fout);
fputc('=', fout);
break;
}
ch = c3 & 0x3f;
fputc(B64Chars[ch], fout);
linelen += 4;
}
fputc('\n', fout);
}
int mutt_write_mime_header (BODY *a, FILE *f)
{
PARAMETER *p;
char buffer[STRING];
char *t;
int len;
int tmplen;
fprintf (f, "Content-Type: %s/%s", TYPE (a->type), a->subtype);
if (a->parameter)
{
len = 25 + strlen (a->subtype); /* approximate len. of content-type */
p = a->parameter;
while (p)
{
fputc (';', f);
buffer[0] = 0;
rfc822_cat (buffer, sizeof (buffer), p->value, tspecials);
tmplen = strlen (buffer) + strlen (p->attribute) + 1;
if (len + tmplen + 2 > 76)
{
fputs ("\n\t", f);
len = tmplen + 8;
}
else
{
fputc (' ', f);
len += tmplen + 1;
}
fprintf (f, "%s=%s", p->attribute, buffer);
p = p->next;
}
}
fputc ('\n', f);
if (a->encoding != ENC7BIT)
fprintf(f, "Content-Transfer-Encoding: %s\n", ENCODING (a->encoding));
if (a->description)
fprintf(f, "Content-Description: %s\n", a->description);
if (a->use_disp && (a->disposition == DISPATTACH || a->filename))
{
fprintf (f, "Content-Disposition: %s", DISPOSITION (a->disposition));
if (a->filename)
{
/* Strip off the leading path... */
if ((t = strrchr (a->filename, '/')))
t++;
else
t = a->filename;
buffer[0] = 0;
rfc822_cat (buffer, sizeof (buffer), t, tspecials);
fprintf (f, "; filename=%s", buffer);
}
fputc ('\n', f);
}
/* Do NOT add the terminator here!!! */
return (ferror (f) ? -1 : 0);
}
int mutt_write_mime_body (BODY *a, FILE *f)
{
char boundary[SHORT_STR